package com.xyre.client.framework.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import java.lang.reflect.Field;

/**
 * 桌面图标的上角标
 *
 * @author 792793182@qq.com 2015-10-09.
 */
public final class BadgeNumberUtil {

    private static final String TAG = BadgeNumberUtil.class.getSimpleName();

    private BadgeNumberUtil() {

    }

    public static void changeBadgeNumber(Context context, int number) {
        DebugLog.d(TAG, "Build.MANUFACTURER = " + Build.MANUFACTURER);

        if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
            xiaoMi(context, number);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
            samsumg(context, number);
        } else if (Build.MANUFACTURER.toLowerCase().contains("sony")) {
            sony(context, number);
        } else if (Build.MANUFACTURER.toLowerCase().contains("huawei")) {
            huawei(context, number);
        } else if (Build.MANUFACTURER.toLowerCase().contains("vivo")) {
            vivo(context, number);
        } else {
            Toast.makeText(context, "Not Support", Toast.LENGTH_LONG).show();
        }
    }

    private static void xiaoMi(Context context, int number) {
        try {
            Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
            Object miuiNotification = miuiNotificationClass.newInstance();
            Field field = miuiNotification.getClass().getDeclaredField("messageCount");
            field.setAccessible(true);
            field.set(miuiNotification, number);// 设置信息数-->这种发送必须是miui 6才行
        } catch (Exception e) {
            DebugLog.e(TAG, "xiaoMi()", e);

            //MIUI 6之前的版本
            Intent intent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
            intent.putExtra("android.intent.extra.update_application_component_name", context.getPackageName() + "/" + getLauncherActivityName(context));
            intent.putExtra("android.intent.extra.update_application_message_text", number);
            context.sendBroadcast(intent);
        }
    }

    private static void sony(Context context, int number) {
        boolean isShow = true;
        if ("0".equals(number)) {
            isShow = false;
        }
        Intent intent = new Intent();
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isShow);//是否显示
        intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", getLauncherActivityName(context));//启动页
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", number);//数字
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名
        context.sendBroadcast(intent);

        Toast.makeText(context, "Sony," + "isSendOk", Toast.LENGTH_LONG).show();
    }

    private static void samsumg(Context context, int number) {
        Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
        intent.putExtra("badge_count", number);//数字
        intent.putExtra("badge_count_package_name", context.getPackageName());//包名
        intent.putExtra("badge_count_class_name", getLauncherActivityName(context)); //启动页
        context.sendBroadcast(intent);

        Toast.makeText(context, "Samsumg," + "isSendOk", Toast.LENGTH_LONG).show();
    }

    private static void vivo(Context context, int number) {
        Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
        intent.putExtra("packageName", context.getPackageName());
        intent.putExtra("className", getLauncherActivityName(context));
        intent.putExtra("notificationNum", number);
        context.sendBroadcast(intent);

        Toast.makeText(context, "vivo," + "isSendOk", Toast.LENGTH_LONG).show();
    }

    private static void huawei(Context context, int number) {
        Bundle bundle = new Bundle();
        bundle.putString("package", context.getPackageName());
        bundle.putString("class", getLauncherActivityName(context));
        bundle.putInt("badgenumber", number);
        Uri uri = Uri.parse("content://com.huawei.android.launcher.settings/badge/");
        context.getContentResolver().call(uri, "change_badge", null, bundle);

        Toast.makeText(context, "huawei," + "isSendOk", Toast.LENGTH_LONG).show();
    }

    /**
     * 获取启动的Activity的完整路径
     *
     * @param context 上下文
     */
    private static String getLauncherActivityName(Context context) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setPackage(context.getPackageName());
        ActivityInfo activityInfo = intent.resolveActivityInfo(context.getPackageManager(), 0);
        DebugLog.d(TAG, activityInfo.name);
        return activityInfo.name;
    }
}
